﻿using System;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using SharpDX.XAudio2;
using SynergyLib.Wave;

namespace XAudio2Controls
{
    public class WavePatchControl : XA2PatchBase
    {
        internal enum WaveTypes
        { 
            Sine,
            Square,
            Saw,
        }

        #region Singleton Pattern
        private static WavePatchControl instance;
        public static WavePatchControl Instance
        {
            get
            {
                if (instance == null)
                    instance = new WavePatchControl();
                return instance;
            }
        }
        #endregion Singleton Pattern

        #region Properties
        public GraphingEffect MasterGrapher
        { get; set; }
        internal static CurrentlySelectedTab CurrentTab { get; set; }
        #endregion Properties 

        #region Constructor
        static WavePatchControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(WavePatchControl), new FrameworkPropertyMetadata(typeof(XA2PatchBase)));
        }

        #endregion Consructor

        #region Public Methods
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            GetTemplateChildren();
        }

        public override string ToString()
        {
            return "Wave Machine";
        }
        #endregion Public Methods

        #region Private Methods

        private static SpectrumAnalyzer GetASpectrumAnalyzer()
        {
            return new SpectrumAnalyzer()
            {
                FFTComplexity = FFTDataSize.FFT2048,
                BarCount = 128,
                BarSpacing = .5,
                PeakFallDelay = 10,
                MinHeight = 100,
                Background = System.Windows.Media.Brushes.Black,
            };
        }

        #endregion Private Methods

        #region Event Handlers

        protected override void ButtonNewPatch_Click(object sender, RoutedEventArgs e)
        {
            if (ListBoxPatches.ItemsSource == null)
                ListBoxPatches.ItemsSource = Patches;
            ListBoxPatches.ItemStringFormat = ToString();
            XAudio2Controls.XA2TabPanelBase newWavePatch = new XAudio2Controls.WavePatchTabPanel();

            newWavePatch.PropertyChanged += newPatch_PropertyChanged;
            newWavePatch.SelectedTabChanged += newPatch_SelectedTabChanged;
            Patches.Add(newWavePatch);
        }
        public void newPatch_SelectedTabChanged(object sender, RoutedEventArgs e)
        {
            if (Patches != null)
            {
                foreach (XA2TabPanelBase tpb in Patches)
                {
                    if (tpb.Tabs != null)
                    {
                        foreach (TabItem ti in tpb.Tabs)
                        {
                            if ((WavePatchControl.CurrentlySelectedTab)ti.Tag == CurrentTab)
                            {
                                ti.IsSelected = true;
                            }
                            else
                            {
                                ti.IsSelected = false;
                            }
                        }
                    }
                }
            }
        }
        
        void newPatch_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "WaveType":
                    ListBoxPatches.ItemStringFormat = null;
                    ListBoxPatches.ItemStringFormat = XAudio2Controls.WavePatchTabPanel.NameProperty.ToString();
                    break;
            }
        }
        #endregion Event Handlers
    }
}
